package com.cutefool.zero.code.dictionary.web;

import com.cutefool.core.kits.snowflake.SnowFlake;
import com.cutefool.core.res.PageResponse;
import com.cutefool.core.res.Response;
import com.cutefool.dsl.mybatis.core.dsl.builder.DeleteDsl;
import com.cutefool.dsl.mybatis.core.dsl.builder.InsertDsl;
import com.cutefool.dsl.mybatis.core.dsl.builder.QueryDsl;
import com.cutefool.dsl.mybatis.core.dsl.builder.UpdateDsl;
import com.cutefool.dsl.mybatis.core.dsl.condition.Conditioning;
import com.cutefool.dsl.mybatis.core.dsl.operation.CuteCurdOperation;
import com.cutefool.dsl.mybatis.core.page.Paging;
import com.cutefool.zero.code.dictionary.domain.DicDefine;
import com.cutefool.zero.code.dictionary.dto.DicDefineDTO;
import jakarta.annotation.Resource;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

;

/**
 * 相关接口
 *
 * @author : 271007729@qq.com
 */
@RestController
public class DicDefineController {
    @Resource
    private SnowFlake snowFlake;

    @Resource
    private CuteCurdOperation<DicDefine> cuteCurdOperation;

    /**
     * 新增
     *
     * @param dto	 dto
     * @return Response<Long>
     **/
    @PostMapping("/add/dic-defines")
    public Response<Long> create(@RequestBody @Validated DicDefineDTO dto) {
        InsertDsl<DicDefine> entity = InsertDsl.newInstance(DicDefine.instance()).entity(dto, (d, s) -> d.setId(snowFlake.nextId()));
        this.cuteCurdOperation.create(entity);
        return Response.ok(entity.getEntity().getId());
    }

    /**
     * 批量新增
     *
     * @param dtoList	 dtoList
     * @return Response<Boolean>
     **/
    @PostMapping("/add/batch/dic-defines")
    public Response<Boolean> create(@RequestBody @Validated List<DicDefineDTO> dtoList) {
        InsertDsl<DicDefine> entities = InsertDsl.newInstance(DicDefine.instance()).entities(dtoList, (d, s) -> d.setId(snowFlake.nextId()));
        this.cuteCurdOperation.create(entities);
        return Response.ok();
    }

    /**
     * 更新
     *
     * @param id	 id
     * @param dto	 dto
     * @return Response<Long>
     **/
    @PostMapping("/update/dic-defines/{id}")
    public Response<Long> update(@PathVariable Long id, @RequestBody @Validated DicDefineDTO dto) {
        UpdateDsl<DicDefine> updateDsl = UpdateDsl.newInstance(DicDefine.instance()).entity(dto);
        this.cuteCurdOperation.update(updateDsl);
        return Response.ok(id);
    }

    /**
     * 删除
     *
     * @param id	 id
     * @return Response<Boolean>
     **/
    @PostMapping("/delete/dic-defines/{id}")
    public Response<Boolean> delete(@PathVariable Long id) {
        DeleteDsl<DicDefine> deleteDsl = DeleteDsl.newInstance(DicDefine.instance(), (where) -> where.and(DicDefine.instance().id(), SqlBuilder.isEqualTo(id)));
        this.cuteCurdOperation.delete(deleteDsl);
        return Response.ok();
    }

    /**
     * 删除
     *
     * @param ids	 ids
     * @return Response<Boolean>
     **/
    @PostMapping("/delete/batch/dic-defines")
    public Response<Boolean> delete(@RequestBody List<Long> ids) {
        DeleteDsl<DicDefine> deleteDsl = DeleteDsl.newInstance(DicDefine.instance(), (where) -> where.and(DicDefine.instance().id(), SqlBuilder.isIn(ids)));
        this.cuteCurdOperation.delete(deleteDsl);
        return Response.ok();
    }

    /**
     * 查询（通过ID）
     *
     * @param id	 id
     * @return Response<DicDefine>
     **/
    @GetMapping("/info/dic-defines/{id}")
    public Response<DicDefine> byId(@PathVariable Long id) {
        QueryDsl<DicDefine> queryDsl = QueryDsl.newInstance(DicDefine.instance(), where -> where.and(DicDefine.instance().id(), SqlBuilder.isEqualTo(id)));
        Optional<DicDefine> optional = this.cuteCurdOperation.get(queryDsl);
        return Response.ok(optional.orElse(null));
    }

    /**
     * 查询（列表）
     *
     * @param query	 query
     * @return Response<List<DicDefine>>
     **/
    @GetMapping("/list/dic-defines")
    public Response<List<DicDefine>> list(Conditioning query) {
        QueryDsl<DicDefine> queryDsl = QueryDsl.newInstance(DicDefine.instance(), query);
        List<DicDefine> entities = this.cuteCurdOperation.list(queryDsl);
        return Response.ok(entities);
    }

    /**
     * 查询（分页）
     *
     * @param query	 query
     * @param paging	 paging
     * @return Response<PageResponse<DicDefine>>
     **/
    @GetMapping("/page/dic-defines")
    public Response<PageResponse<DicDefine>> page(Conditioning query, Paging paging) {
        QueryDsl<DicDefine> queryDsl = QueryDsl.newInstance(DicDefine.instance(), query, paging);
        PageResponse<DicDefine> response = this.cuteCurdOperation.page(queryDsl);
        return Response.ok(response);
    }
}
